package com.lonwan.demo.rest;

import com.lonwan.demo.dto.BookDto;
import com.lonwan.demo.dto.BookUploadDto;
import com.lonwan.demo.util.ResultHelper;
import com.lonwan.demo.vo.BookVO;
import com.lonwan.demo.vo.CommonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.concurrent.CompletableFuture;

/**
 * @author susc
 * @title: SwaggerTestController
 * @projectName knife4j-demo
 * @description:
 *
 * @date 2021/9/299:43
 */
@RestController
@RequestMapping("book")
@Api(tags = "book接口")
public class SwaggerTestController {

    @GetMapping("/info")
    @ApiOperation("获取info接口")
    public CommonResult<String> getBookInfo(BookDto bookDto){
        return ResultHelper.success("成功");
    }

    @GetMapping("/detail")
    @ApiOperation("根据id获取book详情接口")
    public CommonResult<BookVO> getBookInfo2(@ApiParam(value = "bookid",required = true) String id){
        BookVO bookVO = new BookVO();
        bookVO.setId(id);
        bookVO.setName("book测试名称");
        return ResultHelper.success("成功", bookVO);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据id删除book接口")
    public CommonResult<String> deleteBook(@ApiParam(value = "bookid",required = true, type = "data")String id){
        return ResultHelper.success("成功");
    }

    @PostMapping(value = "/upload", produces = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传book附件接口",notes = "上传附件方法1")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", paramType="form", value = "临时文件", dataType="file", required = true),
            @ApiImplicitParam(name="userId", value="用户ID", dataTypeClass = Integer.class, required = true)
    })
    public CommonResult<BookVO> uploadBook(@ApiParam(value = "附件") MultipartFile file, HttpServletRequest request){
        if(null == file){
            throw new NullPointerException("file is null!");
        }
        BookVO bookVO = new BookVO();
        bookVO.setName("测试上传");
        return ResultHelper.success("成功", bookVO);
    }

    @PostMapping(value = "/upload2",consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传book附件接口2", notes = "上传附件方法2")
    public CommonResult<BookVO> uploadBook2(BookUploadDto bookUploadDto){
        MultipartFile file = bookUploadDto.getFile();
        if(null == file){
            throw new NullPointerException("file is null!");
        }
        BookVO bookVO = new BookVO();
        bookVO.setName("测试上传");
        return ResultHelper.success("成功", bookVO);
    }


}
